package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.constant.StatusConstant;
import com.sky.dto.SetmealDTO;
import com.sky.dto.SetmealPageQueryDTO;
import com.sky.entity.Dish;
import com.sky.entity.Setmeal;
import com.sky.entity.SetmealDish;
import com.sky.exception.DeletionNotAllowedException;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealDishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.result.PageResult;
import com.sky.service.SetmealService;
import com.sky.vo.DishVO;
import com.sky.vo.SetmealVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

@Service
@Slf4j
public class SetmealServiceImpl implements SetmealService {

    @Autowired
    private SetmealMapper setmealMapper;

    @Autowired
    private SetmealDishMapper setmealDishMapper;
    @Autowired
    private DishMapper dishMapper;


    /**
     * 新增套餐
     * @param setmealDTO
     */

    @Override
    @Transactional
    public void addSetmeal(SetmealDTO setmealDTO) {
        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(setmealDTO, setmeal);
        //新增基础属性,回填setmealId
        setmealMapper.insert(setmeal);


        //新增setmealDishes的setmealId
        List<SetmealDish> setmealDishes = setmealDTO.getSetmealDishes();
        if (setmealDishes != null && !setmealDishes.isEmpty()) {
            setmealDishes.forEach(setmealDish -> {
                setmealDish.setSetmealId(setmeal.getId());
            });
        }

        setmealDishMapper.insertBatch(setmealDishes);

    }

    @Override
    public PageResult page(SetmealPageQueryDTO setmealPageQueryDTO) {
        PageHelper.startPage(setmealPageQueryDTO.getPage(), setmealPageQueryDTO.getPageSize());

        Page<Setmeal> setmealPage = setmealMapper.page(setmealPageQueryDTO);



        return new PageResult(setmealPage.getTotal(), setmealPage.getResult());
    }

    /**
     * 根据id查询套餐
     * @param id
     * @return
     */
    @Override
    public SetmealVO selectSetmealById(Long id) {
        //查询的是setmeal表   查setmeal_dish表
        Setmeal setmeal =  setmealMapper.selectSetmealById(id);

        List<SetmealDish> setmealDishs =  setmealDishMapper.selectBySetmealId(id);

        SetmealVO setmealVO = new SetmealVO();

        BeanUtils.copyProperties(setmeal, setmealVO);

        setmealVO.setSetmealDishes(setmealDishs);


        return setmealVO;
    }

    /**
     * 更新套餐
     * @param setmealDTO
     */
    @Override
    public void updateSetmeal(SetmealDTO setmealDTO) {
        //更新套餐之前要将之前的setmeal_dish全部删除
        //启售中的套餐不能修改没有编写
        Long setmealId = setmealDTO.getId();
        setmealDishMapper.deleteBySetmealId(setmealId);

        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(setmealDTO, setmeal);

        setmealMapper.update(setmeal);

        List<SetmealDish> setmealDishes = setmealDTO.getSetmealDishes();

        //没有设置setmeal_id
        setmealDishes.forEach(setmealDish -> {
            setmealDish.setSetmealId(setmealId);
        });

        if (setmealDishes != null && !setmealDishes.isEmpty()) {

            setmealDishMapper.insertBatch(setmealDishes);

        }

    }

    /**
     * 启用或者停售套餐
     * @param status
     * @param id
     */
    @Override
    public void enableOrDisbale(Integer status, Long id) {
        Setmeal setmeal = Setmeal.builder().id(id).status(status).build();

        setmealMapper.update(setmeal);
    }


    /**
     * 根据ids批量删除
     * @param ids
     */

    @Override
    @Transactional
    public void deleteBatchByIds(List<Long> ids) {
        //删除前要进行判断是不是起售中的套餐
        ids.forEach(id -> {
            Setmeal setmeal = setmealMapper.selectSetmealById(id);
            if (setmeal.getStatus() == StatusConstant.ENABLE) {
                throw new DeletionNotAllowedException("启售中的套餐不允许删除");
            }

        });

        //删除套餐，同时要将setmeal_dish中关联的一并删除
        setmealMapper.deleteBatch(ids);

       ids.forEach(id -> {
           setmealDishMapper.deleteBySetmealId(id);
       });

    }

    @Override
    public List<SetmealVO> list(Setmeal setmealfind) {
        List<SetmealVO> setmealVOS = new ArrayList<>();

        List<Setmeal> setmeals = setmealMapper.selectByCategoryId(setmealfind);

        setmeals.forEach(setmeal -> {
            SetmealVO setmealVO = new SetmealVO();
            BeanUtils.copyProperties(setmeal, setmealVO);
            List<SetmealDish> setmealDishes = setmealDishMapper.selectBySetmealId(setmeal.getId());
            setmealVO.setSetmealDishes(setmealDishes);
            setmealVOS.add(setmealVO);
        });
        return setmealVOS;



    }

    @Override
    public List<SetmealDish> getDishsById(Long id) {

        List<SetmealDish> setmealDishes = setmealDishMapper.selectBySetmealId(id);
        return setmealDishes;
    }


}
